package com.ndp.fb.mq.service.schedulerservice.business;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.ndp.fb.enums.adgroupstatus.AdGroupStatus;
import com.ndp.fb.fbagent.service.AdGroupFBService;
import com.ndp.fb.mq.api.service.schedulerservice.business.AdGroupGCService;
import com.ndp.fb.rdb.api.AdRdbService;
import com.ndp.fb.rdb.api.EntrustAccountRdbService;
import com.ndp.fb.rdb.model.AdGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.Map;

/**
 * Created by jacky.cheng on 2015/6/18.
 */
@Service
public class AdGroupGCServiceImpl implements AdGroupGCService {

    @Reference
    private AdRdbService adGroupRdbService;
    @Reference
    private EntrustAccountRdbService entrustAccountRdbService;
    @Reference
    private AdGroupFBService adGroupFBService;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public void adGroupDeleteCallBack(Map<String, String> equipment, String response) {
        if (response == null || !response.contains("success")) {
            logger.error("GC回调错误：" + response);
            return;
        }
        AdGroup adGroup = JSONObject.parseObject(equipment.get("adGroup"), AdGroup.class);
        updateLocalAdGroupStatus(adGroup);
    }

    private void updateLocalAdGroupStatus(AdGroup adGroup) {
        adGroupRdbService.updateAdGroupStatus(adGroup.getAdGroupId(), AdGroupStatus.DELETED.getStatus());
        //记录本地更新AdGroup的操作
        AdGroup adBefore = new AdGroup();
        BeanUtils.copyProperties(adGroup, adBefore);
        adBefore.setAdGroupStatus(AdGroupStatus.DELETED.getStatus());
    }




}
